package com.stylefeng.guns.modular.netcar.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.DispatchPsnCarMapper;
import com.stylefeng.guns.modular.netcar.model.DispatchPsnCar;
import com.stylefeng.guns.modular.netcar.service.IDispatchPsnCarService;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 调度员可调度车辆 Service 实现 
 * @author sasazaixian
 * @Date 2018-11-15 10:29
 */
@Service
public class DispatchPsnCarServiceImpl extends ServiceImpl<DispatchPsnCarMapper,DispatchPsnCar> implements IDispatchPsnCarService {

    @Resource
    private DispatchPsnCarMapper dispatchPsnCarMapper;

    /**
     * 删除调度员可调度车辆
     * @param dispatchPsnCar
     * @return     
     */
    @Transactional
    public int deleteDispatchPsnCar(DispatchPsnCar dispatchPsnCar,String token)throws Exception{
        if(dispatchPsnCar==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.dispatchPsnCarMapper.deleteDispatchPsnCar(dispatchPsnCar);
    }
    /**
     * 批量删除调度员可调度车辆
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchDispatchPsnCar(String[] ids,String token)throws Exception{
        return this.dispatchPsnCarMapper.deleteBatchDispatchPsnCar(ids);
    }
    /**
     * 查询调度员可调度车辆列表
     * @param dispatchPsnCar 调度员可调度车辆对象
     * @return
     */
    public List<DispatchPsnCar> selectListDispatchPsnCar(DispatchPsnCar dispatchPsnCar,String token)throws Exception{
        return  this.dispatchPsnCarMapper.selectListDispatchPsnCar(dispatchPsnCar);
    }
    /**
	 * 分页查询调度员可调度车辆列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListDispatchPsnCar(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(dispatchPsnCarMapper.selectListDispatchPsnCar(page,(DispatchPsnCar)pager.getObj()));
        List<DispatchPsnCar> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param dispatchPsnCar 调度员可调度车辆对象
     * @return 
     */
    public  List<DispatchPsnCar> selectListDispatchPsnCarForUpdate(DispatchPsnCar dispatchPsnCar)throws Exception{
        return  this.dispatchPsnCarMapper.selectListDispatchPsnCarForUpdate(dispatchPsnCar);
    }
    /**
     * 通过主键id查询的调度员可调度车辆列表
     * @param id 主键id
     * @return     
     */
    public  DispatchPsnCar findDispatchPsnCarById(String id,String token)throws Exception{
        return  this.dispatchPsnCarMapper.findDispatchPsnCarById(id);
    }

    /**
     * 新增调度员可调度车辆数据
     * @param dispatchPsnCar  调度员可调度车辆对象
     * @return
     */
    @Transactional
    public int insertDispatchPsnCar(DispatchPsnCar dispatchPsnCar,String token) throws Exception {
        if(dispatchPsnCar.getViIdArry()==null || dispatchPsnCar.getViIdArry().trim().length()<10){
            throw new RuntimeException("车辆信息异常");
        }
        if(dispatchPsnCar.getSuId()==null && (dispatchPsnCar.getSuIdArry()==null || dispatchPsnCar.getSuIdArry().trim().length()<1)){
            throw new RuntimeException("线路不能为空！");
        }

        String[] viIdArry=dispatchPsnCar.getViIdArry().trim().split(",");
        String [] suIdArry=null;
        if(dispatchPsnCar.getSuId()==null){
            suIdArry= dispatchPsnCar.getSuIdArry().split(",");
        }else{
            suIdArry=new String []{dispatchPsnCar.getSuId().toString()};
        }
        for(String suId:suIdArry){
            dispatchPsnCar.setSuId(suId);
            for(String viId:viIdArry){
                if(viId!=null && viId.trim().length()>10) {
                    dispatchPsnCar.setDpcId(IdWorker.get32UUID());
                    dispatchPsnCar.setViId(viId.trim());
                    this.dispatchPsnCarMapper.insertDispatchPsnCar(dispatchPsnCar);
                }
            }
        }
       return 0;
    }
    
    /**
     * 更新调度员可调度车辆数据
     * @param dispatchPsnCar 调度员可调度车辆对象
     * @return     
     */
    @Transactional
    public int updateDispatchPsnCar(DispatchPsnCar dispatchPsnCar,String token) throws Exception {     	
        return  this.dispatchPsnCarMapper.updateDispatchPsnCar(dispatchPsnCar);
    }    
    /**
     * 查询调度员可调度车辆返回key-Value属性
     * @param dispatchPsnCar 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsDispatchPsnCar(DispatchPsnCar dispatchPsnCar,String token) throws Exception {
        List<DispatchPsnCar> listDispatchPsnCar=this.dispatchPsnCarMapper.selectListDispatchPsnCar(dispatchPsnCar);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(DispatchPsnCar item:listDispatchPsnCar){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getDpcId());
            opts.setValue(item.getDpcId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
